package com.zsk.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.page.BasePageParams;
import com.zsk.shop.common.constant.RedisKeys;
import com.zsk.shop.common.utils.HttpContext;
import com.zsk.shop.common.utils.PageWithSqlUtil;
import com.zsk.shop.common.utils.RedisUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.ShopCollection;
import com.zsk.shop.entity.ShopGoodsSku;
import com.zsk.shop.enums.EnumDataDictionaryKey;
import com.zsk.shop.mapper.ShopCollectionMapper;
import com.zsk.shop.model.results.ShopContentListResults;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ys
 * @date 2021年12月21日 15:15
 */
@Service
public class ShopCollectionService extends ServiceImpl<ShopCollectionMapper, ShopCollection> {

    @Resource
    private ShopGoodsSkuService shopGoodsSkuService;

    public BaseResponse<List<ShopContentListResults>> getUserCollectionList(BasePageParams params){
        String userNo = HttpContext.getUserNo();
        List<ShopContentListResults> results = this.baseMapper.getUserCollectionList(userNo, PageWithSqlUtil.getIndex(params.getPage(), params.getLimit()), PageWithSqlUtil.getSize(params.getPage(), params.getLimit()));
        for (ShopContentListResults obj: results) {
            List<ShopGoodsSku> shopGoodsSkuList = shopGoodsSkuService.listByGoodsNo(obj.getGoodsNo());
            obj.setStock(shopGoodsSkuList.stream().mapToLong(ShopGoodsSku::getStock).sum());
            // 验证入口来源 返回对应价格
            ShopGoodsSku shopGoodsSkuLowPrice = shopGoodsSkuList.stream().sorted(Comparator.comparing(ShopGoodsSku::getPrice)).collect(Collectors.toList()).get(0);
            if (obj.getGoodsOrderType().equals(1)) {
                obj.setPrice(shopGoodsSkuLowPrice.getPrice().toString());
            } else if (obj.getGoodsOrderType().equals(2)) {
                obj.setPrice(shopGoodsSkuLowPrice.getPrice().toString());
                obj.setZeroPrice("0.00");
            } else if (obj.getGoodsOrderType().equals(3)) {
                obj.setPrice(shopGoodsSkuLowPrice.getPrice().toString());
                obj.setVipPrice(shopGoodsSkuLowPrice.getVipPrice().toString());
            } else if (obj.getGoodsOrderType().equals(4)) {
                obj.setPrice(shopGoodsSkuLowPrice.getPrice().toString());
                obj.setReturnPoints(shopGoodsSkuLowPrice.getReturnPoints());
            }
            if (ToolUtil.isNotEmpty(shopGoodsSkuLowPrice.getSpecialPrice())) {
                obj.setSpecialPrice(shopGoodsSkuLowPrice.getSpecialPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            } else {
                obj.setSpecialPrice(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
            }
        }
        return BaseResponse.success("收藏列表", results, this.baseMapper.getUserCollectionCount(userNo));
    }

    public synchronized BaseResponse saveCollection(String goodsNo, Integer goodsOrderType){
        String userNo = HttpContext.getUserNo();
        if(this.baseMapper.queryCollectionByContentNo(userNo, goodsNo, goodsOrderType) > 0){
            return BaseResponse.error("请勿重复收藏!");
        }
        ShopCollection obj = ShopCollection.builder().goodsNo(goodsNo).goodsOrderType(goodsOrderType).userNo(HttpContext.getUserNo()).createTime(new Date()).build();
        this.save(obj);
        return BaseResponse.success();
    }

    public BaseResponse cancelCollection(String goodsNo, Integer goodsOrderType){
        this.baseMapper.deleteCollectionByUser(HttpContext.getUserNo(), goodsNo, goodsOrderType);
        return BaseResponse.success();
    }

    public boolean isCollection(String userNo, String goodsNo, Integer goodsOrderType){
        LambdaQueryWrapper<ShopCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCollection::getUserNo, userNo);
        queryWrapper.eq(ShopCollection::getGoodsNo, goodsNo);
        queryWrapper.eq(ShopCollection::getGoodsOrderType, goodsOrderType);
        queryWrapper.last("limit 1");
        ShopCollection obj = this.getOne(queryWrapper);
        if(ToolUtil.isNotEmpty(obj)){
            return true;
        }
        return false;
    }

}
